/**
 * ContentContextAdaptor
 * 
 * The idea behind this family of classes is to adapt content contexts to something more UPNP
 * Specific.
 *
 *
 * @author Scot McSweeney-Roberts
 * @version 0.1
 * 
 *
 */

package uk.co.sheitgeist.fums.server.upnp.adaptors

import uk.co.sheitgeist.fums.server.content.store.*
import uk.co.sheitgeist.fums.server.content.context.*
import uk.co.sheitgeist.fums.server.content.transport.*

import com.cidero.upnp.*

import java.util.logging.Logger

public class ContentContextAdaptor {

	private static Logger logger = Logger.getLogger("uk.co.sheitgeist")

	ContentContext contentContext

	public ContentContextAdaptor(ContentContext contentContext) {
		this.contentContext = contentContext
	}

	/**
	 * toCDSObject
	 *
	 * This returns a root object for this content context
	 *
	 */
	public CDSObject toCDSObject() {
		CDSStorageFolder folder = new CDSStorageFolder()
		folder.id = "${this.contentContext.id}:/"
		folder.parentId = "0"
		folder.title = this.contentContext.name
		folder.creator = "UNKNOWN"
		return (CDSObject)folder
	}
	//alternate name for above
	public CDSObject getRoot() {
		return this.toCDSObject()
	}

	public CDSObject getChild(String objectId) {
		ContextNode node = contentContext.getObject(objectId)
		return contextNodeToCDSObject(node)
	}

	public CDSObjectList getDirectChildren(String objectId) {
		ContextNode node = contentContext.getObject(objectId)
		CDSObjectList list = new CDSObjectList()
		node.directChildren.each { child ->
			list.add contextNodeToCDSObject(child)			
		}
		return list
	}

	private CDSObject contextNodeToCDSObject(ContextNode contextNode) {
	//	logger.info "contextNodeToCDSObject ${contextNode.id}"
		CDSObject object
	//	logger.info "isFolder? ${contextNode.isFolder}"
		if (contextNode.isFolder) {
				CDSStorageFolder folder = new CDSStorageFolder()
				folder.id = "${this.contentContext.id}:${contextNode.id}"
				folder.parentId = "${this.contentContext.id}:${contextNode.parent.id}"
				folder.title = contextNode.name
				folder.creator = "UNKNOWN"
				object = folder
		} else {
			switch (contextNode.primaryType) {
					case "image":
						CDSImageItem imageItem = new CDSImageItem()
						imageItem.id = "${this.contentContext.id}:${contextNode.id}"
						imageItem.parentId = "${this.contentContext.id}:${contextNode.id}"
						imageItem.title = contextNode.name
						imageItem.creator = "UNKNOWN"

						imageItem.addResource(new CDSResource(contextNode.url))

						object = imageItem

						break;
					case "audio": 
						CDSAudioItem audioItem = new CDSAudioItem()
						audioItem.id = "${this.contentContext.id}:${contextNode.id}"
						audioItem.parentId = "${this.contentContext.id}:${contextNode.id}"
						audioItem.title = contextNode.name
						audioItem.creator = "UNKNOWN"

						audioItem.addResource(new CDSResource(contextNode.url))

						object = audioItem
						break;
					case "video":
						CDSVideoItem videoItem = new CDSVideoItem()
						videoItem.id = "${this.contentContext.id}:${contextNode.id}"
						videoItem.parentId = "${this.contentContext.id}:${contextNode.id}"
						videoItem.title = contextNode.name
						videoItem.creator = "UNKNOWN"
						
						CDSResource res = new CDSResource(contextNode.url)
						res.protocolInfo="http-get:*:video/mp4:*"
						videoItem.addResource(res)

						object = videoItem

						break;
					default: logger.info "cc::cNTCDSO primaryType - ${contextNode.primaryType}"
						object = new CDSItem()
			}
		}
		return object
	}
}
